package com.hourses.controller;

import com.hourses.dto.AssignRoleDto;
import com.hourses.page.Page;
import com.hourses.pojo.CurrentUser;
import com.hourses.pojo.Result;
import com.hourses.pojo.Role;
import com.hourses.pojo.User;
import com.hourses.service.impl.RoleServiceImpl;
import com.hourses.service.impl.UserServiceImpl;
import com.hourses.utils.TokenUtils;
import com.hourses.utils.WarehouseConstants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.repository.query.ReturnedType;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description:
 * @Author:LiTao
 * @Date:2025/4/28 12:21
 */
@RequestMapping("/user")
@RestController
public class UserController {
    //展示用户列表  http://localhost:3000/api/user/user-list
    //            ?userCode=&userType=&userState=&pageSize=5&pageNum=1&totalNum=0
    @Autowired
    private UserServiceImpl userService;
    @GetMapping("/user-list")
    public Result userList(User user,Page page){
        Page resultPage = userService.selectUserList(user, page);
        return Result.ok(resultPage);
    }

    //添加用户功能的接口   http://localhost:3000/api/user/addUser
    @Autowired
    private TokenUtils tokenUtils;
    @PostMapping("/addUser")
    public Result addUser(@RequestBody User user, @RequestHeader(WarehouseConstants.HEADER_TOKEN_NAME)String token){
        CurrentUser currentUser = tokenUtils.getCurrentUser(token);
        user.setCreateBy(currentUser.getUserId());
        Result result = userService.insertUser(user);
        return result;
    }

    //启用或者禁用用户功能的接口  http://localhost:3000/api/user/updateState
    @RequestMapping("/updateState")
    public Result updateState(@RequestBody User user){
        Result result = userService.updateUserState(user);
        return result;
    }

    //在用户列表界面给用户分配角色时展示用户已有角色 http://localhost:3000/api/user/user-role-list/{userId}
    @Autowired
    private RoleServiceImpl roleService;
    @GetMapping("/user-role-list/{userId}")
    public Result getUserRolesByUserId(@PathVariable("userId")Integer userId){
        List<Role> roles = roleService.selectUserRolesByUserRole(userId);
        return Result.ok(roles);
    }

    //给用户分配角色  http://localhost:3000/api/user/assignRole
    @RequestMapping("/assignRole")
    public Result assignRole(@RequestBody AssignRoleDto assignRoleDto){
        Result result = userService.setUserRoles(assignRoleDto.getUserId(),assignRoleDto.getRoleCheckList());
        return result;
    }

    //删除单个用户   http://localhost:3000/api/user/deleteUser/36
    @RequestMapping("/deleteUser/{userId}")
    public Result deleteUserByUserId(@PathVariable Integer userId){
        List<Integer> userIds = new ArrayList<>();
        userIds.add(userId);
        Result result = userService.updateIsdeleteByUserIds(userIds);
        return result;
    }

    //删除多个用户   http://localhost:3000/api/user/deleteUserList
    @RequestMapping("/deleteUserList")
    public Result deleteUsersByUserId(@RequestBody List<Integer> userIds){
        Result result = userService.updateIsdeleteByUserIds(userIds);
        return result;
    }

    //修改用户信息  http://localhost:3000/api/user/updateUser
    @RequestMapping("/updateUser")
    public Result updateUserName(@RequestBody User user,@RequestHeader(WarehouseConstants.HEADER_TOKEN_NAME) String token){
        CurrentUser currentUser = tokenUtils.getCurrentUser(token);
        user.setUpdateBy(currentUser.getUserId());
        Result result = userService.updateUserNameByUserId(user);
        return result;
    }

    //重置用户密码   http://localhost:3000/api/user/updatePwd/28
    @RequestMapping("/updatePwd/{userId}")
    public Result resetPwd(@PathVariable("userId") Integer userId){
        Result result = userService.resetPwd(userId);
        return result;
    }
}
